Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support for scoped registry urls #4

Merged
merged 2 commits into from
Jun 30, 2018
Merged

Conversation

killerfurbel
Copy link

I got across this when checking one of my projects which contains modules from SAP (@sap/ prefix), those modules are hosted on npm.sap.com. To be able to npm install those modules, you need to add this setting to the npm config (for example, to the user config):

@sap:registry = "https://npm.sap.com"

see https://docs.npmjs.com/misc/scope#associating-a-scope-with-a-registry

I was looking for a way to get these npm settings and came across npm-conf, but this module mainly relied on a dependency config-chain to get the configuration. I checked this one and it said:

This module is frozen.
In general, I'd recommend using rc instead, but as npm depends on this, it cannot be changed.

So I went with rc, which does the job.

rc has the following dependencies:

┌───────────────────────────┬─────────┬──────────────┬──────────────────────────────────────────────────┬────────────────────────────┐
│ Package                   │ Size    │ Updated      │ License                                          │ Dependencies               │
├───────────────────────────┼─────────┼──────────────┼────────────┬─────────────────────────────────────┼────────────────────────────┤
│ [email protected]                  │ 6.98 KB │ a month ago  │ Permissive │ (BSD-2-Clause OR MIT OR Apache-2.0) │ deep-extend@^0.6.0,        │
│                           │         │              │            │                                     │ ini@~1.3.0,                │
│                           │         │              │            │                                     │ minimist@^1.2.0,           │
│                           │         │              │            │                                     │ strip-json-comments@~2.0.1 │
├───────────────────────────┼─────────┼──────────────┼────────────┼─────────────────────────────────────┼────────────────────────────┤
│ [email protected]                 │ 3.8 KB  │ 7 months ago │ Permissive │ ISC                                 │                            │
├───────────────────────────┼─────────┼──────────────┼────────────┼─────────────────────────────────────┼────────────────────────────┤
│ [email protected]            │ 7.54 KB │ 3 years ago  │ Permissive │ MIT                                 │                            │
├───────────────────────────┼─────────┼──────────────┼────────────┼─────────────────────────────────────┼────────────────────────────┤
│ [email protected] │ 2.22 KB │ 2 years ago  │ Permissive │ MIT                                 │                            │
├───────────────────────────┼─────────┼──────────────┼────────────┼─────────────────────────────────────┼────────────────────────────┤
│ [email protected]         │ 3.54 KB │ a month ago  │ Permissive │ MIT                                 │                            │
└───────────────────────────┴─────────┴──────────────┴────────────┴─────────────────────────────────────┴────────────────────────────┘

Now rc is used to get the config (global and project specific), the module key is checked for a scope using a regex (starting with @, containing a /). If there is a scope, try to use the registry url of the scope, fallback to the official registry.npmjs.org

npm config is fine when the scoped registry URL does not end with a /, but since we just concaternate the module name, we need to add a / to the url if it doesn't end with one...

see https://docs.npmjs.com/misc/scope#associating-a-scope-with-a-registry
using node module rc to get the config (global and project specific) and use the registry url of the scope if existing, fallback to the official registry.npmjs.org
@delfrrr
Copy link
Owner

delfrrr commented Jun 28, 2018

Hi! Thank you for the pull request and detailed explanation of how it works! Overall the approach of using of rc looks good to me. I will check the code soon.

Copy link
Owner

@delfrrr delfrrr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall great, please check the comments;
If possible run it through eslint, it has a config.
Thank you!

@@ -70,6 +72,11 @@ module.exports = function getPackageDetails(
return Promise.resolve(null);
}
const key = `${name}@${versionLoose}`;
const scope = (/(@[^\/]+)\/.*/.test(name) && /(@[^\/]+)\/.*/.exec(name)[1]) || ``;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suggest to use https://github.com/npm/npm-package-arg (because npm uses it)
or alternatively copy logic from it to be on safe side 😉

this.scope = name[0] === '@' ? name.slice(0, name.indexOf('/')) : undefined
// scoped packages in couch must have slash url-encoded, e.g. @foo%2Fbar
this.escapedName = name.replace('/', '%2f')

It's difficult to say if your regexp works exactly. the same.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just changed the parsing to the same logic, thanks for the hint

var registryUrl = (scope && npmConfig[`${scope}:registry`]) || npmConfig[`registry`];
if(registryUrl.charAt(registryUrl.length - 1) !== `/`) {
registryUrl += `/`;
}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@@ -70,6 +72,11 @@ module.exports = function getPackageDetails(
return Promise.resolve(null);
}
const key = `${name}@${versionLoose}`;
const scope = (/(@[^\/]+)\/.*/.test(name) && /(@[^\/]+)\/.*/.exec(name)[1]) || ``;
var registryUrl = (scope && npmConfig[`${scope}:registry`]) || npmConfig[`registry`];
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let please

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed together with the remaining eslint errors

@delfrrr delfrrr merged commit 0c2afa8 into delfrrr:master Jun 30, 2018
@delfrrr
Copy link
Owner

delfrrr commented Jun 30, 2018

Thank you @killerfurbel !

@delfrrr
Copy link
Owner

delfrrr commented Jun 30, 2018

Published as 1.6.0 on npm and GitHub https://github.com/delfrrr/npm-consider/releases

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants